package com.wtwd.campus.jdzc.service.impl;

import com.wtwd.campus.entity.FenceInfo;
import com.wtwd.campus.jdzc.dao.FenceInfoMapper;
import com.wtwd.campus.jdzc.service.FenceService;
import com.wtwd.campus.utils.GouldUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: ldaoliang
 * @DateTime: 2022/2/15 10:10
 * @Description:
 */
@Service
public class FenceServiceImpl implements FenceService {

    @Autowired
    private FenceInfoMapper fenceInfoMapper;
    @Autowired
    private GouldUtil gouldUtil;

    public List<FenceInfo> getJdzcFenceInfoListByStuId(Integer stuId){
        return fenceInfoMapper.getJdzcFenceInfoListByStuId(stuId);
    }

    @Override
    public boolean fenceNameIsRepeat(String fenceName, Integer studentId) {
        return fenceInfoMapper.getJdzcFenceInfoByStuIdAndFenceName(studentId,fenceName) != null;
    }

    @Override
    public Integer addJdzcWxFence(FenceInfo fenceInfo) {
            if(StringUtils.isBlank(fenceInfo.getLocation())){
                //获取中心点位置
                String location = null;
                try {
                    location = gouldUtil.getAMapByLngAndLat(fenceInfo.getLongitude().toString(), fenceInfo.getLatitude().toString());
                    fenceInfo.setLocation(location);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            fenceInfo.setCreate_time(System.currentTimeMillis());
            Integer i = fenceInfoMapper.addFence(fenceInfo);
            return i;
    }

    @Override
    public FenceInfo getFenceInfoByFenceId(int fenceId) {
        return fenceInfoMapper.getJdzcFenceInfoByFenceId(fenceId);
    }

    public Integer deleteFenceInfo(Integer fenceId) {
        return fenceInfoMapper.deleteFenceIfo(fenceId);
    }

    public Integer modifyFenceInfo(FenceInfo fenceInfo) {
        //获取中心点位置
        String location = null;
        try {
            location = gouldUtil.getAMapByLngAndLat(fenceInfo.getLongitude().toString(), fenceInfo.getLatitude().toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        fenceInfo.setLocation(location);
        return fenceInfoMapper.modifyFenceInfo(fenceInfo);
    }
}
